package com.angel.controller;

import com.angel.bean.Branches;
import com.angel.bean.Employee;
import com.angel.bean.VipInfo;
import com.angel.bean.dto.VipInfoDto;
import com.angel.service.OtherlogService;
import com.angel.service.VipInfoService;
import com.angel.shiro.SystemAdminAuthorizingRealm;
import com.angel.utils.AdminUserUtils;
import com.angel.utils.LogUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/vipInfoController")
public class VipInfoController {
    //// /vipInfoController/findVipInfoList
    @Resource
    private VipInfoService vipInfoService;
    @Autowired
    private OtherlogService otherlogService;

    /**
     * 添加vipInfo
     *
     * @return
     * @RequiresPermissions("user") 权限控制器必须登录才能访问
     */
    @RequiresPermissions("user")
    @RequestMapping("/addVipInfo")
    @ResponseBody
    public Object addVipInfo(VipInfo vipInfo) {
        Map<Object, Object> map = new HashMap<Object, Object>();
        vipInfo.setState(1);
        int result = vipInfoService.insert(vipInfo);
        if (result != 0) {
            map.put("result", "success");
        }
        return map;
    }

    /***
     * 更新
     *
     * @RequiresPermissions("user") 权限控制器必须登录才能访问
     * @paramVipInfo 前端传入id值
     * @return
     */
    @RequiresPermissions("user")
    @RequestMapping("/updateVipInfo")
    @ResponseBody
    public Object updateVipInfo(VipInfo vipInfo) {
        Map<Object, Object> map = new HashMap<Object, Object>();
        int result = vipInfoService.updateVipNo(vipInfo);
        if (result != 0) {
            map.put("result", "success");
        }
        return map;
    }

    /**
     * 查询单个信息
     *
     * @return
     * @RequiresPermissions("user") 权限控制器必须登录才能访问
     */
    @RequiresPermissions("user")
    @RequestMapping("/findOneVipInfo")
    @ResponseBody
    public Object findOneVipInfo(VipInfo vipInfo) {
        Map<Object, Object> map = new HashMap<Object, Object>();
        System.out.println("-------------进入vip查找单张vip卡");
        VipInfoDto vipinf = vipInfoService.findOneDto(vipInfo);
        map.put("vipinf", vipinf);
        return map;
    }

    /**
     * 查询信息
     *
     * @return
     * @RequiresPermissions("user") 权限控制器必须登录才能访问
     */
    @RequiresPermissions("user")
    @RequestMapping("/findVipInfoList")
    public String findVipCard(Model model, VipInfo vipInfo) {

        System.out.println("--------进入vipInfo，查询所有用户");
        vipInfo.setState(1);
        List<VipInfo> vipInfoList = vipInfoService.findAll(vipInfo);
        if (vipInfoList != null) {
            model.addAttribute("vipInfoList", vipInfoList);
        }
        return "vipInfoEntry";//无页面
    }


    /***
     * 充值功能
     * @param vipInfoDto
     * @return
     */
    @RequestMapping("/userrecharge")
    @ResponseBody
    public Map userRecharge(VipInfoDto vipInfoDto) {
        Map map = new HashMap();
        int sumCharge = vipInfoDto.getSumCharge();
        int count  = 0;
        boolean flag =false;
       if (vipInfoDto.getRemainTime()!=null && vipInfoDto.getRemainTime() != 0){
            count = vipInfoDto.getRemainTime();
           if (count == 0 || count < 0){
             flag =true;
           }
       }
        map.put("success", false);
        VipInfo vipInfo = new VipInfo();
        vipInfo.setUserId(vipInfoDto.getUserId());
        vipInfo = vipInfoService.findOne(vipInfo);
        Employee employee = null;
        if (vipInfo != null) {
            SystemAdminAuthorizingRealm.Principal principal = AdminUserUtils.getPrincipal();
            employee = principal.getEmployee();
            if (employee.getBranchesId() != vipInfo.getBranId()) {
                map.put("msg", "该会员卡不属于本店，请核实！");
            } else {
               Map result = null;
                try {
                    if (flag){
                        map.put("msg", "充值次数有误！请核实信息");
                        LogUtils.InsertLog(employee,otherlogService,"充值信息有有误，充值失败");
                        return map;
                    }
                  result =  vipInfoService.recharge(vipInfo,employee,sumCharge,count);
                  //充值结果
                  boolean resu = (boolean) result.get("success");
                  if (resu){
                    //充值成功
                      map.put("success",true);
                      String restring = (String) result.get("msg");
                      map.put("msg",restring);
                  }else {
                      String restring = (String) result.get("msg");

                      map.put("msg",restring);
                      //充值失败，结果里面msg信息
                  }
                } catch (Exception e) {
                    map.put("msg", "充值失败");
                    e.printStackTrace();
                }
            }
        } else {
            map.put("msg", "该会员信息不存在");
        }
        LogUtils.InsertLog(employee,otherlogService,"充值信息："+map.get("msg"));
        return map;
    }

}
